package edu.nf.takeoutdemo.web.user;

import edu.nf.takeoutdemo.commons.base.BaseController;
import edu.nf.takeoutdemo.commons.result.Result;
import edu.nf.takeoutdemo.model.dto.ShoppingCartDTO;
import edu.nf.takeoutdemo.model.entity.ShoppingCart;
import edu.nf.takeoutdemo.service.ShoppingCartService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @author zjl
 * @date 2025/10/29
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/user/shoppingCart")
@Slf4j
public class ShoppingCartController extends BaseController {

    private final ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public Result<?> save(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("添加购物车：商品信息为：{}", shoppingCartDTO);
        shoppingCartService.saveShoppingCart(shoppingCartDTO);
        return success();
    }

    @GetMapping("/list")
    public Result<List<ShoppingCart>> list(){
        List<ShoppingCart> shoppingCartList = shoppingCartService.showShoppingCart();
        return success(shoppingCartList);
    }

    @DeleteMapping("/clean")
    public Result<?> clean(){
        shoppingCartService.cleanShoppingCart();
        return success();
    }

    @PostMapping("/sub")
    public Result<?> sub(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("删除购物车中一个商品，商品：{}", shoppingCartDTO);
        shoppingCartService.subShoppingCart(shoppingCartDTO);
        return success();
    }
}
